<template>
    <div class="app-container">
        <h4>个人中心</h4>
        <div class="upload-container">
            <div class="icon">
                <img :src="tittleimg" alt="头像" width="150px" height="150px">
            </div>
            <el-button type="primary" @click.native="handleupload">上传头像</el-button>
        </div>
    
        <div class="info-container">
            <el-form autoComplete="on" :model="temp" ref="temp" label-position="left">
                <el-form-item label="登录账号" label-width="100px" prop="account">
                    <el-input v-model="temp.account" autoComplete="off"></el-input>
                </el-form-item>
                <el-form-item label="登录密码" label-width="100px" prop="passwd">
                    <el-input type="password" v-model="temp.passwd" autoComplete="on"></el-input>
                    <el-button type="text" style="margin-left:70px;" @click.native="handleModPasswd">更改密码</el-button>
                </el-form-item>
                <el-form-item label="真实姓名" label-width="100px" prop="name">
                    <el-input v-model="temp.name" autoComplete="on"></el-input>
                </el-form-item>
                <el-form-item label="联系电话" label-width="100px" prop="contactPhone">
                    <el-input v-model="temp.contactPhone" autoComplete="on"></el-input>
                </el-form-item>
                <el-form-item label="所属公司" label-width="100px" prop="contactPhone">
                    <el-input v-model="temp.companyId" autoComplete="on"></el-input>
                </el-form-item>
                <el-form-item label="所属部门" label-width="100px" prop="departId">
                    <el-input v-model="temp.departId" autoComplete="on"></el-input>
                </el-form-item>
                <el-form-item label="部门角色" label-width="100px" prop="departId">
                    <el-input name="roles" v-model="temp.roles" autoComplete="on"></el-input>
                </el-form-item>
            </el-form>
    
        </div>
    
        <!--dailog更改密码-->
        <el-dialog size="10%" title="更改密码" style="margin-left:30%;margin-right:30%" v-model="editFormVisible" :close-on-click-modal="false">
            <div class="modialog">
                <el-form :model="mima" :rules="ruleUp" ref="mima" label-width="100px">
                    <el-form-item label="原有密码" prop="oldPassword">
                        <el-input v-model="mima.oldPassword" style="width:150px;height:30px" type="password"  auto-complete="off"></el-input>
                    </el-form-item>
                    <el-form-item label="新密码" prop="newPassword">
                        <el-input v-model="mima.newPassword" style="width:150px;height:30px"  type="password" auto-complete="off"></el-input>
                    </el-form-item>
                    <el-form-item label="确认密码" prop="againPassword">
                        <el-input v-model="mima.againPassword" style="width:150px;height:30px" type="password" auto-complete="off"></el-input>
                    </el-form-item>
    
                    <el-form-item>
                        <div  class="dialog-footer">
                            <el-button type="primary" style="margin-top:10px;margin-left:10%" @click="handleValid">
                                确认
                            </el-button>
                            <el-button type="primary" style="margin-top:10px;margin-left:10px" @click="editFormVisible=fasle">
                                取消
                            </el-button>
                        </div>
                    </el-form-item>
                </el-form>
            </div>
        </el-dialog>
    
    </div>
</template>
 
 

<style>
.upload-container {
    margin-top: 0px;
    left: 110px;
    width: 170px;
    height: 17 0px;
    float: left
}

.info-container {
    margin-top: 0px;
    width: 20%;
    float: left
}

.modialog {
    margin-top: 10px;
    width: 50%;
}
</style>


<script>
import tittleimg from '@/assets/login_images/tittle.png';
export default {
    data() {
        return {
            tittleimg,
            editFormVisible: false,
            temp: {
                account: '',
                passwd: '',
                name: '',
                contactPhone: '',
                companyId: '',
                departId: '',
                roles: '',
                icon: ''
            },
            mima: {
                oldPassword: '',
                newPassword: ''
            },
            ruleUp: {
                oldPassword: [
                    { required: true, message: '请输入原有密码', trigger: 'blur' }
                ],
                newPassword: [
                    { required: true, message: '请输入新密码', trigger: 'blur' }
                ],
                againPassword: [
                    { required: true, message: '再次输入新密码', trigger: 'blur' }
                ],
            }
        }
    },
    created() {
        this.getUserInfo();
        this.validateCheckPwd();
    },
    methods: {
        showMessage(msg, type) {
            this.$message({
                message: msg,
                type: type
            });
        },
        getUserInfo() {
            this.$http.get('/user').then((response) => {
                this.temp = response.data;
            })
        },
        handleModPasswd: function () {
            this.mima.oldPassword = '';
            this.mima.newPassword = '';
            this.mima.againPassword = '';
            this.editFormVisible = true;
        },
        handleValid: function (oldPassword, newPassword,againPassword) {
            //判断两次输入密码
               if (this.mima.newPassword !== this.mima.againPassword) {
                   this.$message.error('两次输入密码不一致!')
            } else{
            //发送修改密码请求
            this.$http.put('http://10.247.3.29:5001/user/resetPassword', {
                oldPassword: this.mima.oldPassword,
                newPassword: this.mima.newPassword
            }).then((response) => {        
                if (response.code === 0) {
                    this.editFormVisible = false;
                    this.showMessage('修改成功!', 'success');
                } else {
                    this.showWarning(response.msg, '修改失败');
                }
            }).catch((response) => {
                console.log(response)
            })
         }
        },
    }
};

</script>